const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
const fs = require('fs');

const tokens = ['0x41198c6736d23734E6d17a6F0B4f2D15d2621A3a', //SHARE
                '0xb6ccFA7Ef3a295932536E0988CffD85228cB177c', //sCASH
                '0xbBe6C2337a2543239FF313Bed370e4eAEda268eF', //sHT
                '0x2f92C6e8ed836B5d507251b59d816A55F1B853cC', //sNEO
                '0xb813b811967a77e30B3B1582A578753520C2a4d0', //sFILDA
                '0x22BAD5077579e4437927Bc4394a6B5435df5f39F', //sCAN
                '0x9812C79099418486838a218037D49b2f823F120c', //sNULS
                '0xc9269B837613ffB0ae04123b410A4A0E59402932', //sFLM
                '0x9019CA43C7f6de0C87483EfA9c1Ba65A369F22e4', //sSHARE
                '0xFd981d07CD2cBe039D5A7Ab246C0C92F8ED4FEA8', //vSHARE
                '0x46914A848405fd79a0B937F388936C9798bF6eAC', //sMDX
            ];

async function update() {
    const browser = await puppeteer.launch({
        headless: true,
        defaultViewport: { // 默认窗口大小
            width: 2560,
            height: 1440
        }
    });
    const page = await browser.newPage();
    let total = 0;
    for(var i=0;i<tokens.length;i++) {
        total += await _initCode(tokens[i], browser, page);
        console.log(i + '/' + tokens.length);
    }
    await browser.close();
    console.log(total);
}

async function _initCode(addr, browser, page) {
    let newBrowser = !browser;
    if (newBrowser) {
        browser = await puppeteer.launch({
            headless: true,
            defaultViewport: { // 默认窗口大小
                width: 2560,
                height: 1440
            }
        });
        page = await browser.newPage();
    }

    await page.goto('https://hecoinfo.com/token/' + addr, {timeout: 0});
    const content = await page.content();
    const $ = cheerio.load(content, { decodeEntities: false });
    let txt = $('#ContentPlaceHolder1_tr_tokenHolders div .col-md-8').text().split(' ')[0].replace(/,/g, '');
    console.log(Number(txt));
    if (newBrowser) {
        await browser.close();
    }
    return Number(txt);
}

update();